<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing csv files.
 * @package backend-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Athlete_screening extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        
        $this->load->model('athlete_screening_model');
        $this->load->model('screening_model');
        $this->load->model('user_model');

        $this->load->library('csvreader');
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');

    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        set_time_limit(0);

        $athlete = 1;
        
        $this->csvreader->separator = '|';
        $screenings = $this->csvreader->parse_file('csvs/Athlete Screenings.csv');

        foreach($screenings as $screening)
        {
            
            $user_id = $this->user_model->attribute( 'UserID', array('FirstName' => $screening['FName'], 'LastName' => $screening['LName']) );
            $screening = array(
                'Height' => $screening['Height'],
                'Weight' => $screening['Weight'],
                'LDorsalflexion' => $screening['LDorsalflexion'],
                'LL45' => $screening['LL45'],
                'LR45' => $screening['LR45'],
                'LL135' => $screening['LL135'],
                'LR135' => $screening['LR135'],
                'LL90' => $screening['LL90'],
                'LR90' => $screening['LR90'],
                'LRotation' => $screening['LRotation'],
                'LPower1' => $screening['LPower1'],
                'LPower2' => $screening['LPower2'],
                'LPower3' => $screening['LPower3'],
                'LPower' => $screening['LPower'],
                'LSpeed' => $screening['LSpeed'],
                'LEndurance' => $screening['LEndurance'],
                'RDorsalflexion' => $screening['RDorsalflexion'],
                'RL45' => $screening['RL45'],
                'RR45' => $screening['RR45'],
                'RL135' => $screening['RL135'],
                'RR135' => $screening['RR135'],
                'RL90' => $screening['RL90'],
                'RR90' => $screening['RR90'],
                'RRotation' => $screening['RRotation'],
                'RPower1' => $screening['RPower1'],
                'RPower2' => $screening['RPower2'],
                'RPower3' => $screening['RPower3'],
                'RPower' => $screening['RPower'],
                'RSpeed' => $screening['RSpeed'],
                'REndurance' => $screening['REndurance']
            );
            $screening_id = $this->screening_model->insert( $screening );
            
            if( $user_id && $screening_id )
            {
                $athlete_screening                  = array();
                $athlete_screening['ScreeningID']   = $screening_id;
                $athlete_screening['AthleteID']     = $user_id;
                $athlete_screening['Year']          = 1;
                $athlete_screening['Week']          = $screening['Week'];
                
//                $screenings = $this->athlete_screening_model->count( array( 'ScreeningID' => $screening_id, 'AthleteID' => $user_id ) );
//                $athlete_screening['Week']          = $screenings > 0 ? -24 : -14;
                
                $this->athlete_screening_model->insert( $athlete_screening );
            }
        }

        echo '<pre>';
        print_r($screenings); 
        echo '</pre>';
        exit();
        
    }
}

/* End of file parsecsv.php */
/* Location: ./application/roles/backend-csvreader/controllers/parsecsv.php */